package com.hermit.qmst.UI.main;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.hermit.qmst.R;
import com.hermit.qmst.UI.activity.OpenUrlActivity;
import com.hermit.qmst.request.RequestTask;
import com.hermit.qmst.request.RequestTaskInterface;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.FileUtils;
import com.hermit.qmst.tools.ImageDownLoader;

import org.json.JSONArray;
import org.json.JSONObject;


public class WebSiteActivity extends Fragment {

    private ImageView mImageView;
    private FileUtils mFileUtils;
    private ImageDownLoader mImageDownLoader;
    private String mUrl;
    private View mView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImageDownLoader = new ImageDownLoader(getActivity());
        mFileUtils = new FileUtils();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (mView == null){
            mView = inflater.inflate(R.layout.activity_web_site, container, false);

        }else {

            ViewGroup vp =  (ViewGroup)mView.getRootView();
            vp.removeView(mView);

        }
        return mView;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (mImageView == null) {
            mImageView = (ImageView) getActivity().findViewById(R.id.iv_web);
            mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (mUrl != null){
                        Intent intent = new Intent(getActivity(), OpenUrlActivity.class);
                        intent.putExtra("title","官网");
                        intent.putExtra("url",mUrl);
                        startActivity(intent);
                    }

                }
            });
            getlbAdsFromLocal(null);
        }

        request_web();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);


    }

    @Override
    public void onDetach() {
        super.onDetach();

    }


    //请求精品推荐信息
    private void request_web(){
        String url = Common.submitQueryPicPath();
        String param =   "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><type>website_pic</type><uid>"+Common.mInfoId+"</uid><phone>"+ Common.myPhone +"</phone></data>";
        RequestTask task = new RequestTask(new RequestTaskInterface() {
            @Override
            public void postExecute(String paramString) {

                if (paramString != null) {

                    try {
                        JSONObject jsonObject = new JSONObject(paramString);
                        if ( jsonObject.getInt("status") == 1) {

                            JSONArray dataArr = jsonObject.getJSONArray("data");


                            getlbAdsFromLocal(dataArr);

                        } else {

                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }, url, param, "POST", getActivity());
        task.execute();
    }

    //从本地获取广告
    private void getlbAdsFromLocal( JSONArray dataArr){

        JSONArray array = null;
        if (dataArr == null){//从本地获取保存的数据
            try{
                String str = mFileUtils.readTxt(Common.getDialImageDirPath(),Common.mWebsiteTxtFileName);
                if (str == null){
                    return;
                }


                array = new JSONArray(str);

            }catch (Exception e){
                e.printStackTrace();
            }

        }else {
            array = dataArr;
        }
        //读取图片
        try{
            JSONObject jsonObject = array.getJSONObject(0);
            if (!jsonObject.getString("img").equals("")){

                String imgUrl = jsonObject.getString("img");
                 mUrl = jsonObject.getString("url");
                Bitmap bitmap = mImageDownLoader.downloadImage(Common.getDialImageDirPath(),imgUrl,new ImageDownLoader.onImageLoaderListener() {
                    @Override
                    public void onImageLoader(Bitmap bitmap, String url) {
                        Drawable drawable = new BitmapDrawable(getActivity().getResources(), bitmap);
                        mImageView.setBackgroundDrawable(drawable);
                    }
                });

                if (bitmap != null){

                    Drawable drawable = new BitmapDrawable(getActivity().getResources(), bitmap);
                    mImageView.setBackgroundDrawable(drawable);
                }

            }



        }catch (Exception e){
            e.printStackTrace();
        }

        if (dataArr != null){
            try{
                mFileUtils.saveTxtFile(Common.getDialImageDirPath(),Common.mWebsiteTxtFileName,array.toString());
            }catch (Exception e){
                e.printStackTrace();
            }
        }


    }

}
